package com.tengju.bff.interfaces.app.archive;

import com.tengju.bff.interfaces.app.archive.assembler.ArchiveAssembler;
import com.tengju.bff.interfaces.app.archive.vo.BrandVO;
import com.tengju.bff.interfaces.app.archive.vo.PopupInfoVo;
import com.tengju.bff.interfaces.app.archive.vo.ResumeVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.user.domain.model.archive.resume.BrandId;
import com.tengju.user.domain.model.archive.resume.Resume;
import com.tengju.user.domain.model.archive.resume.ResumeId;
import com.tengju.user.domain.model.archive.resume.ResumeRepository;
import com.tengju.user.domain.model.user.UserId;
import com.tengju.user.domain.shared.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.tengju.bff.interfaces.shared.InterfaceCode.SYSTEM_ERROR;


@RestController
@Api(tags = "我的档案接口")
@RequestMapping(value = "archive")
public class ArchiveController {

//    @Value("${archive.brand}")
//    String brands;

    private final ResumeRepository resumeRepository;

    @Autowired
    public ArchiveController(ResumeRepository resumeRepository) {
        this.resumeRepository = resumeRepository;
    }


    @ApiOperation(value = "查询我的简历档案接口", notes = "查询我的简历档案接口")
    @GetMapping(value = "resume/get", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<ResumeVO> get() {

        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserId userId = servletContext.needUserId();

            Resume resume = resumeRepository.find(userId);

            return ApiResponse.newSuccess(ArchiveAssembler.modelToVo(resume));
        } catch (InterfaceException e) {
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }
    }


    @ApiOperation(value = "保存我的简历档案接口", notes = "保存我的简历档案接口")
    @PostMapping(value = "resume/save")
    public ApiResponse<Boolean> save(@RequestBody ResumeVO resumeVO) {

        try {
            UserServletContext servletContext = ServletContextUtil.getServletContext();
            UserId userId = servletContext.needUserId();

            Resume resume = resumeRepository.find(userId);
            ResumeId resumeId = Optional.ofNullable(resume).map(Resume::getResumeId).orElse(null);

            //目前默认全部品牌选中
            resumeVO.setBrands(BrandId.ALL_BRANDS.keySet());

            if (resume == null) {
                resumeRepository.store(ArchiveAssembler.voToModel(resumeVO, userId, null));
            } else {
                resumeRepository.update(resumeId, ArchiveAssembler.voToModel(resumeVO, userId, resumeId));
            }
            return ApiResponse.newSuccess(true);

        } catch (InterfaceException e) {
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }
    }

//    @ApiOperation(value = "弹窗判断", notes = "弹窗判断")
//    @PostMapping(value = "resume/needPopup")
//    public ApiResponse<PopupInfoVo> needPopup() {
//
//
//        return ApiResponse.newSuccess(new PopupInfoVo());
//    }
//
//    @ApiOperation(value = "获取所有品牌列表", notes = "获取所有品牌列表")
//    @GetMapping(value = "get/brands")
//    public ApiResponse<Map<String, List<BrandVO>>> getAllBrands() {
//
//        List<BrandVO> brandVOS = JsonUtil.toList(brands, new TypeReference<>() {
//        });
//
//        HashMap<String, List<BrandVO>> brandMap = new HashMap<>();
//        brandMap.put("brands",brandVOS);
//
//        return ApiResponse.newSuccess(brandMap);
//    }


}
